/**************************************************************************************
 *  Copyright (c)  2010  Leszek Karcz.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package data;
import java.util.Hashtable;
import java.io.*;
import java.awt.*;

/**************************************************************************************
 * Author(s): Leszek Karcz
 * This class converts all ".png" image files in "Graphics" folder into image objects
 * and store them in a hash table for easy access form all View classes
 **************************************************************************************/
public class Images
{
	private Hashtable<String, Image> images;
	private String folder="src\\data\\graphics\\";

	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Images constructor
	 **************************************************************************************/
	public Images()
	{
		//Initialize the hash table
		images= new Hashtable<String, Image>();
		
		//Create media tracker
		MediaTracker media = new MediaTracker(null);
		
		//Iterate through graphics directory
		File imageFolder = new File(this.folder); 
		String[] children = imageFolder.list();
		if (children != null)
		{
			for (int i=0; i<children.length; i++)
			{
				String imageFile = children[i];
				//If file ends with .png add to media tracker and hash table
				if(imageFile.substring(imageFile.length()-4).equalsIgnoreCase(".png"))
				{
					Image image = Toolkit.getDefaultToolkit().getImage(this.folder + imageFile);
					media.addImage(image, 0);
					try
					{
						media.waitForID(0);
					} catch (Exception e) {}
					if(image != null)
					{
						//Add image to hash table
						this.images.put(imageFile.substring(0,imageFile.length()-4), image);
					}
				}
			}
		}
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Return image object based on file name
	 **************************************************************************************/
	public Image getImage(String fileName)
	{
		return this.images.get(fileName);
	}
}
